package ui;

import Pojo.Book;
import Service.LibraryService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class LibraryManage extends JInternalFrame implements ActionListener {
    private JTextField txtBookId;
    private JTextField txtBookName;
    private JTextField txtBookType;
    private JTextField txtloanReaderId;
    private JTable table;
    private DefaultTableModel dtm;//它使用Vector的Vectors来存储单元格值对象。

    private LibraryService libraryService=new LibraryService();
    public  LibraryManage(){
        super("图书管理",true,true,true,true);//调用父类的构造方法
        setBounds(100,100,678,450);
        getContentPane().setLayout(new BorderLayout());

        JPanel panelTop=new JPanel();
        getContentPane().add(panelTop, BorderLayout.NORTH);

        JLabel labelBookId=new JLabel("图书编号");
        panelTop.add(labelBookId);
        txtBookId=new JTextField();
        txtBookId.setColumns(10);
        panelTop.add(txtBookId);

        JLabel labelBookName=new JLabel("图书名称");
        panelTop.add(labelBookName);
        txtBookName=new JTextField();
        txtBookName.setColumns(10);
        panelTop.add(txtBookName);

        JLabel labelloanReaderId=new JLabel("借阅人");
        panelTop.add(labelloanReaderId);
        txtloanReaderId=new JTextField();
        txtloanReaderId.setColumns(10);
        panelTop.add(txtloanReaderId);

        JButton btnQuery=new JButton("查询");
        btnQuery.addActionListener(this);
        panelTop.add(btnQuery);

        JScrollPane scrollPane=new JScrollPane();
        getContentPane().add(scrollPane,BorderLayout.CENTER);
        String[] header={"图书编号","图书名称","图书作者","借阅人"};

        dtm=new DefaultTableModel(null,header);
        table=new JTable(dtm){
            @Override
            public boolean isCellEditable(int row, int column) {//如果可以编辑row和column的单元格，则返回true。否则，在单元格上调用setValueAt将不起作用。

                return false;
            }
        };
        table.setRowHeight(35);
        scrollPane.setViewportView(table);

        JPanel panelBottom=new JPanel();
        getContentPane().add(panelBottom,BorderLayout.SOUTH);

        JButton btnDel=new JButton("删除借阅信息");
        btnDel.addActionListener(this);
        panelBottom.add(btnDel);

        JButton btnEdit=new JButton("编辑借阅信息");
        btnEdit.addActionListener(this);
        panelBottom.add(btnEdit);

        JButton btnFind=new JButton("查看借阅信息");
        btnFind.addActionListener(this);
        panelBottom.add(btnFind);

        JButton btnAdd=new JButton("添加借阅信息");
        btnAdd.addActionListener(this);
        panelBottom.add(btnAdd);
        loadBook();
        setVisible(true);
    }
    public void loadBook(){
        String BookId=txtBookId.getText();
        String BookName=txtBookName.getText();
        String loanReaderId=txtloanReaderId.getText();
        dtm.setRowCount(0);
        List<Book> list=libraryService.findBookAll(null,null,null,null);
        String[][] data=libraryService.listToArray(list);
        for(int i = 0 ; i < data.length ; i++){
            dtm.addRow(data[i]);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("删除借阅信息")){
            int index=table.getSelectedRow();
            if(index!=-1){
                int result=JOptionPane.showConfirmDialog(this,"确定删除？","系统提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(result == JOptionPane.YES_OPTION){
                    libraryService.delBook(index);
                    JOptionPane.showMessageDialog(this,"删除成功！");
                    loadBook();
                }
            }else{
                JOptionPane.showMessageDialog(this,"请选择需要删除的行！");
                loadBook();
            }
        }else if(e.getActionCommand().equals("编辑借阅信息")){
            int index = table.getSelectedRow();
            if(index != -1){
                Book book =libraryService.findBookById(index);
                new BookEditManger(book,this);
            }else{
                JOptionPane.showMessageDialog(this,"请选择需要编辑的信息！");
                loadBook();
            }
        }else if(e.getActionCommand().equals("查看借阅信息")){
            int index = table.getSelectedRow();
            if (index!=-1){
                Book book =libraryService.findBookById(index);
                new BookCheckManger(book,this);
            }else{
                JOptionPane.showMessageDialog(this,"请选择需要查看的信息！");
                loadBook();
            }

        }else if(e.getActionCommand().equals("添加借阅信息")){
            new BookAddManger();
        }

    }
}